package com.windystory.wslibrary.base.fragment;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.windystory.wslibrary.R;
import com.windystory.wslibrary.base.fragment.lazy.ImmersionOwner;
import com.windystory.wslibrary.base.fragment.lazy.ImmersionProxy;
import com.windystory.wslibrary.net.netstatus.INETWork;
import com.windystory.wslibrary.util.ToastUtil;
import com.windystory.wslibrary.util.event.Event;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * BaseFragment
 * Created by windystory on 2018/1/4.
 */

public abstract class BaseFragment extends Fragment implements INETWork, ImmersionOwner {
    private View rootView;
    public Context context;
    public Activity activity;
    private ProgressDialog mProgressDialog;
    public INETWork inetWork;


    /**
     * fragment中使用沉浸式通过实现ImmersionOwner接口来实现沉浸式
     */
    private ImmersionProxy mImmersionProxy = new ImmersionProxy(this);


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mImmersionProxy.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        onSubscribeRxBus();
        rootView = inflater.inflate(getLayoutResId(), null);
        activity = getActivity();
        context = getActivity();
        inetWork = this;
        initDialog();
        initView(rootView);
        initData();
        initListener();
//        registerNetWorkReceiver();
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mImmersionProxy.onActivityCreated(savedInstanceState);
    }

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 初始化监听器
     */
    protected abstract void initListener();

    protected void initDialog() {

    }


    /**
     * 初始化布局
     *
     * @return
     */
    protected abstract int getLayoutResId();

    /**
     * 初始化View
     *
     * @param v
     */
    protected abstract void initView(View v);


    /**
     * 初始 rxSubscription
     *
     * @author hanhuizhong
     * @date 2016-7-28
     */
    private void onSubscribeRxBus() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusSubscribe(Event eventConfig) {
        onEventDealWith(eventConfig);
    }


    /**
     * 处理EventBus事件
     */
    protected abstract void onEventDealWith(Event eventConfig);


    /**
     * 显示ProgressDialog
     *
     * @param cancelable 是否可以点击取消
     * @param message
     */
    public void showProgress(boolean cancelable, String message) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(activity);
            Drawable drawable = getResources().getDrawable(R.drawable.pic_progressbar_bg);
            mProgressDialog.setIndeterminateDrawable(drawable);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgressDialog.setCancelable(cancelable);
            mProgressDialog.setCanceledOnTouchOutside(false);
            mProgressDialog.setMessage(message);
        }
        mProgressDialog.show();
    }

    /**
     * dismiss ProgressDialog
     */
    public void hideProgress() {
        if (mProgressDialog == null)
            return;

        if (mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    /**
     * 显示toast
     *
     * @param tip
     */
    public void showToast(String tip) {
        ToastUtil.generalToast(activity, tip);
    }


//    //注册监听网络变化
//    private void registerNetWorkReceiver() {
//        NetWorkChange.getInstance().setOnNetWorkChange(home_onChange);
//    }
//
//    //取消监听网络变化
//    private void unregisterNetWorkReceiver() {
//        NetWorkChange.getInstance().delOnNetWorkChange(home_onChange);
//    }
//
//    /**
//     * 网络监听
//     */
//    private NetWorkChange.OnNetWorkChange home_onChange = new NetWorkChange.OnNetWorkChange() {
//        @Override
//        public void onChange(int wifi, int mobile, int none, int oldStatus, int newStatus) {
//            if (newStatus == none) {
//                //没有网络
//                LogUtil.e("没有网络");
//                inetWork.withOutNetWork();
//            }
//            if (newStatus == mobile) {
//                //移动网络
//                LogUtil.e("移动网络");
//                inetWork.withMobileNetWork();
//            }
//            if (newStatus == wifi) {
//                //wifi网络
//                LogUtil.e("wifi网络");
//                inetWork.withWiFiNetWork();
//                if (oldStatus == mobile) {
//                    //从移动网络切换到wifi网络
//                    LogUtil.e("移动网络");
//                    inetWork.withMobile2WiFiNetWork();
//                }
//            }
//        }
//    };


    /**
     * 无网
     */
    public void withOutNetWork() {
    }

    /**
     * 有移动网
     */
    public void withMobileNetWork() {
    }

    /**
     * 有wifi网
     */
    public void withWiFiNetWork() {

    }


    /**
     * 移动网转WIFI网
     */
    public void withMobile2WiFiNetWork() {

    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mImmersionProxy.onConfigurationChanged(newConfig);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        mImmersionProxy.onHiddenChanged(hidden);
    }


    /**
     * 懒加载，在view初始化完成之前执行
     */
    public void onLazyBeforeView() {
    }

    /**
     * 懒加载，在view初始化完成之后执行
     */
    public void onLazyAfterView() {
    }

    /**
     * Fragment用户可见时候调用
     */
    public void onVisible() {
    }

    /**
     * Fragment用户不可见时候调用
     */
    public void onInvisible() {

    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        mImmersionProxy.setUserVisibleHint(isVisibleToUser);
    }


    @Override
    public void onResume() {
        super.onResume();
        mImmersionProxy.onResume();

    }

    @Override
    public void onPause() {
        super.onPause();
        mImmersionProxy.onPause();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        unregisterNetWorkReceiver();
        mImmersionProxy.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    public void initImmersionBar() {

    }

    @Override
    public boolean immersionBarEnabled() {
        return false;
    }

}
